package com.xianrenit.xianrenpuzi.common.handler.interceptor;

import com.xianrenit.xianrenpuzi.model.entity.h5.UserInfo;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class SpzxServiceAuthInterceptor implements HandlerInterceptor {
    @Resource
    RedisTemplate redisTemplate;
    public static ThreadLocal<UserInfo> THREAD_LOCAL = new ThreadLocal();
    @Override
    public boolean preHandle(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        UserInfo userInfo = (UserInfo) redisTemplate.opsForValue().get("spzx:user:login:" + token);
        THREAD_LOCAL.set(userInfo);
        return true;
    }

    @Override
    public void afterCompletion(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response, Object handler, Exception ex) throws Exception {
        THREAD_LOCAL.remove();
    }
}
